package cn.csc.module.product.controller.admin.stock;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.stock.vo.*;
import cn.csc.module.product.convert.stock.SkuStockConvert;
import cn.csc.module.product.dal.dataobject.stock.SkuStockDO;
import cn.csc.module.product.service.stock.SkuStockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 库存信息")
@RestController
@RequestMapping("/product/sku-stock")
@Validated
public class SkuStockController {

    @Resource
    private SkuStockService skuStockService;

    @PostMapping("/create")
    @ApiOperation("创建库存信息")
    @PreAuthorize("@ss.hasPermission('product:sku-stock:create')")
    public CommonResult<BigInteger> createSkuStock(@Valid @RequestBody SkuStockCreateReqVO createReqVO) {
        return success(skuStockService.createSkuStock(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新库存信息")
    @PreAuthorize("@ss.hasPermission('product:sku-stock:update')")
    public CommonResult<Boolean> updateSkuStock(@Valid @RequestBody SkuStockUpdateReqVO updateReqVO) {
        skuStockService.updateSkuStock(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除库存信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:sku-stock:delete')")
    public CommonResult<Boolean> deleteSkuStock(@RequestParam("id") BigInteger id) {
        skuStockService.deleteSkuStock(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得库存信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:sku-stock:query')")
    public CommonResult<SkuStockRespVO> getSkuStock(@RequestParam("id") BigInteger id) {
        SkuStockDO skuStock = skuStockService.getSkuStock(id);
        return success(SkuStockConvert.INSTANCE.convert(skuStock));
    }

    @GetMapping("/list")
    @ApiOperation("获得库存信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:sku-stock:query')")
    public CommonResult<List<SkuStockRespVO>> getSkuStockList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SkuStockDO> list = skuStockService.getSkuStockList(ids);
        return success(SkuStockConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得库存信息分页")
    @PreAuthorize("@ss.hasPermission('product:sku-stock:query')")
    public CommonResult<PageResult<SkuStockRespVO>> getSkuStockPage(@Valid SkuStockPageReqVO pageVO) {
        PageResult<SkuStockDO> pageResult = skuStockService.getSkuStockPage(pageVO);
        return success(SkuStockConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出库存信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:sku-stock:export')")
    @OperateLog(type = EXPORT)
    public void exportSkuStockExcel(@Valid SkuStockExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SkuStockDO> list = skuStockService.getSkuStockList(exportReqVO);
        // 导出 Excel
        List<SkuStockExcelVO> datas = SkuStockConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "库存信息.xls", "数据", SkuStockExcelVO.class, datas);
    }

}
